<?php

/**
 * MVM_MALL 网上商店系统 商品品牌管理
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-07-14 $
 * $Id: brand.inc.php  www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
if ($action=='list'){
    require_once MVMMALL_ROOT.'./include/pager.class.php';
    $total_count = $db->counter("{$tablepre}brand_table");
    $page        = $page ? (int)$page:1;
	$list_num    = 10;
	$rowset      = new Pager($total_count,$list_num,$page);
	$from_record = $rowset->_offset();
    $result      = $db->query("SELECT * FROM `{$tablepre}brand_table` ORDER BY `train` LIMIT $from_record, $list_num");
    while ($rt   = $db->fetch_array($result)) {
        $rt['brief']  = cut_str($rt['brief'],20);
        $rt['edit']   = "admincp.php?module=$module&action=edit&uid=$rt[id]";
		$rt['del']    = "admincp.php?module=$module&action=del&uid=$rt[id]&per=ajax";
		$rt['logo']   && $rt['logo']= "<img src=\"$rt[logo]\" border=\"0\"/>";
    	$brand_rt[]   = $rt;
    }
    $page_list = $rowset->link("admincp.php?module=$module&action=$action&page=");
    require_once template('brand');
    footer();
} elseif ($action=='add') {
    if($setp==1 && $_POST) {
        $brandname = dhtmlchars($brandname);
        $keywords  = dhtmlchars($keywords);
        $brief     = dhtmlchars($brief);
        $weburl    = dhtmlchars($weburl);
        $order     = (int)$order;
        if ($_FILES['logo_file']['name']!=''){
            require_once MVMMALL_ROOT.'./include/upfile.class.php';
            $rowset = new upfile('gif,jpg,png,bmp','./images/brand/');
            $logo   = $rowset->upload('logo_file');
        }
        $query = "INSERT INTO `{$tablepre}brand_table` SET
                      brandname   = '$brandname',
                      logo        = '$logo',
                      keywords    = '$keywords',
                      brand_desc  = '$brand_desc',
                      brief       = '$brief',
                      weburl      = '$weburl',
                      train       = '$train'
                      ";
        $db->query($query);
        $cache->put_cache('brand');
        admin_msg('success','admincp.php?module=brand&action=list');
    } else {
        require_once template('brand_add');
        footer();
    }
} elseif ($action=='edit' && is_numeric($uid)) {
    $brand_rt = $db->get_one("SELECT * FROM `{$tablepre}brand_table` WHERE id='$uid'");
    if($setp==1 && $_POST) {
        $brandname = dhtmlchars($brandname);
        $keywords  = dhtmlchars($keywords);
        $brief     = dhtmlchars($brief);
        $weburl    = dhtmlchars($weburl);
        $order     = (int)$order;
        if ($_FILES['logo_file']['name']!=''){
            require_once MVMMALL_ROOT.'./include/upfile.class.php';
            $rowset = new upfile('gif,jpg,png,bmp','./images/brand/');
            $logo   = $rowset->upload('logo_file');
            $brand_rt['logo'] && file_unlink($brand_rt['logo']);
        }else {
            $logo  =  $brand_rt['logo'];
        }
        $query = "UPDATE {$tablepre}brand_table SET
                      brandname   = '$brandname',
                      logo        = '$logo',
                      keywords    = '$keywords',
                      brand_desc  = '$brand_desc',
                      brief       = '$brief',
                      weburl      = '$weburl',
                      train       = '$train'
                      WHERE  id   ='$uid'
                      ";
        $db->query($query);
        $cache->put_cache('brand');
        admin_msg('success','admincp.php?module=brand&action=list');
    } else {
      @extract($brand_rt,EXTR_OVERWRITE);
      require_once template('brand_add');
      footer();   
     }
} elseif ($action=='del' && is_numeric($uid)) {
    $brand_rt = $db->get_one("SELECT id,logo FROM `{$tablepre}brand_table` WHERE id ='$uid'");
    $brand_rt['logo'] && file_unlink($brand_rt['logo']);
    $db->query("DELETE FROM `{$tablepre}brand_table` WHERE id = '$brand_rt[id]'");
    $cache->put_cache('brand');
    $per!='ajax' && admin_msg('success',dreferer());
} else {
    admin_msg('pass_worng');
}